package org.cug.uav.gcs.ui;

import org.cug.uav.gcs.map.MapView;
import org.cug.uav.gcs.waypoints.WayPoint;
import org.cug.uav.gcs.waypoints.WayPointTableProvider;
import org.cug.uav.gcs.waypoints.WayPoints;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.IOpenListener;
import org.eclipse.jface.viewers.OpenEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;

public class WayPointsEditorDlg extends Dialog {

	MapView mapView;
	protected Object result;
	protected Shell shell;
	public static WayPoints wayPoints;
	public static TableViewer tableViewer;
	
	public Point getMousePixelXY() {
		return mousePixelXY;
	}

	public void setMousePixelXY(Point mousePixelXY) {
		this.mousePixelXY = mousePixelXY;
	}

	private Point mousePixelXY;
	private Table table;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public WayPointsEditorDlg(Shell parent, int style,MapView _mapView) {
		super(parent, style);
		setText("SWT Dialog");
		wayPoints=_mapView.wayPoints;
		mapView=_mapView;
		createContents();
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		tableViewer.refresh();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.SHELL_TRIM);
		shell.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				
			}
		});
		shell.setSize(566, 300);
		shell.setText("\u8DEF\u5F84\u70B9\u7F16\u8F91");
		
		ToolBar toolBar = new ToolBar(shell, SWT.FLAT | SWT.RIGHT);
		toolBar.setBounds(0, 0, 558, 32);
		
		ToolItem upBtn = new ToolItem(toolBar, SWT.NONE);
		upBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				wayPoints.moveUp(table.getSelectionIndex());
				mapView.mapCanvas.redraw();
				tableViewer.refresh();
			}
		});
		upBtn.setText("\u4E0A\u79FB");
		
		ToolItem downBtn = new ToolItem(toolBar, SWT.NONE);
		downBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				wayPoints.moveDown(table.getSelectionIndex());
				mapView.mapCanvas.redraw();
				tableViewer.refresh();
			}
		});
		downBtn.setText("\u4E0B\u79FB");
		
		ToolItem addBtn = new ToolItem(toolBar, SWT.NONE);
		addBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				WayPoint point=new WayPoint(table.getItemCount()+1,mapView.locLU,new Point(40, 50),mapView.levelOfDetail);
				wayPoints.addWayPoint(point);
				mapView.mapCanvas.redraw();
				tableViewer.refresh();
				//tableViewer.setInput(point);
				
				
			}
		});
		addBtn.setText("\u6DFB\u52A0");
		
		ToolItem deleteBtn = new ToolItem(toolBar, SWT.NONE);
		deleteBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int slectIndex=table.getSelectionIndex();;
				if (slectIndex!=-1) {
					wayPoints.deleteWayPoint(slectIndex);
					tableViewer.refresh();
					mapView.mapCanvas.redraw();
				}
			}
		});
		deleteBtn.setText("\u5220\u9664");
		
		tableViewer = new TableViewer(shell, SWT.BORDER | SWT.FULL_SELECTION);
		tableViewer.setColumnProperties(new String[] {});
		tableViewer.addOpenListener(new IOpenListener() {
			public void open(OpenEvent arg0) {
			}
		});
		tableViewer.setContentProvider(new WayPointTableProvider());
		tableViewer.setLabelProvider(new WayPointTableProvider());
		tableViewer.setInput(wayPoints.getwaypoints());
		table = tableViewer.getTable();
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		table.setBounds(10, 38, 419, 218);
		
		final TableColumn tableViewerColumn = new TableColumn(table, SWT.NONE);
		//TableColumn columnNum = tableViewerColumn.getColumn();
		tableViewerColumn.setWidth(72);
		tableViewerColumn.setText("\u5E8F\u53F7");
		
		final TableColumn tableViewerColumn_2 = new TableColumn(table, SWT.NONE);
		//TableColumn columnLatitude = tableViewerColumn_2.getColumn();
		tableViewerColumn_2.setWidth(100);
		tableViewerColumn_2.setText("\u7EAC\u5EA6");
		
		final TableColumn tableViewerColumn_1 = new TableColumn(table, SWT.NONE);
		//TableColumn columnLongitude = tableViewerColumn_1.getColumn();
		tableViewerColumn_1.setWidth(100);
		tableViewerColumn_1.setText("\u7ECF\u5EA6");
		
		final TableColumn tableViewerColumn_3 = new TableColumn(table, SWT.NONE);
		//TableColumn columnAltitude = tableViewerColumn_3.getColumn();
		tableViewerColumn_3.setWidth(100);
		tableViewerColumn_3.setText("\u9AD8\u5EA6");

	}
}
